package gui.product;

import core.Product;
import core.ProductFacade;
import gui.common.Controller;
import gui.common.IView;
import gui.common.SizeUnits;

public abstract class ProductController extends Controller
{
	protected ProductView _pView;
	protected ProductData _pData;
	protected static ProductFacade _pFacade = ProductFacade.instance();
	
	protected ProductController(IView view, ProductData pData) 
	{
		super(view);
		_pView = (ProductView)view;
		_pData = pData;
	}
	
	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() 
	{
		assert _pData != null;
			_pView.setBarcode(_pData.getBarcode());
			_pView.setDescription(_pData.getDescription());
			_pView.setShelfLife(_pData.getShelfLife());
			_pView.setSizeUnit(SizeUnits.fromString(_pData.getSizeUnit()));
			_pView.setSizeValue(_pData.getSizeValue());
			_pView.setSupply(_pData.getSupply());
	}
	
	/**
	 * This method is called when any of the fields in the
	 * add product view is changed by the user.
	 */
	public void valuesChanged() 
	{
		checkIfDescriptionChanged();
		checkIfAmountChanged();
		checkIfShelfLifeChanged();
		checkIf3MoSupplyChanged();
		checkIfCanCreateProduct();
	}
	
	private void checkIfCanCreateProduct() 
	{
		//TODO: maybe the ProductFacade can take the ProductData object here.
		int shelfLife = 0;
		int supply = 0;
		try
		{
			shelfLife = Integer.parseInt(_pData.getShelfLife());
			supply = Integer.parseInt(_pData.getSupply());
		}
		catch (Exception e)
		{
			_pView.enableOK(false);
			return;
		}
		if (Product.canCreate(_pData.getBarcode(), 
										_pData.getDescription(), 
										_pData.getSizeValue(),
										_pData.getSizeUnit(), 
										shelfLife, 
										supply))
		{
			_pView.enableOK(true);
		}
		else
		{
			_pView.enableOK(false);
		}
		
	}

	private void checkIf3MoSupplyChanged() 
	{
		String view3Mo = _pView.getSupply();
		if (!view3Mo.equals(_pData.getSupply()))
		{
			_pData.setSupply(view3Mo);
		}
	}

	private void checkIfShelfLifeChanged() 
	{
		String viewShelfLife = _pView.getShelfLife();
		if (!viewShelfLife.equals(_pData.getShelfLife()))
		{
			_pData.setShelfLife(viewShelfLife);
		}
	}

	private void checkIfAmountChanged() 
	{
		String viewSize = _pView.getSizeValue();
		String viewUnit = _pView.getSizeUnit().toString();
		String oldSize = _pData.getSizeValue();
		String oldUnit = _pData.getSizeUnit();
		if (!viewSize.equals(oldSize) || viewUnit != oldUnit)
		{
			if (viewUnit.equals(SizeUnits.Count.toString()))
			{
				viewSize = "1";
				_pView.setSizeValue("1");
				_pView.enableSizeValue(false);
			}
			else if (oldUnit.equals(SizeUnits.Count.toString()) &&
					!oldUnit.equals(viewUnit))
			{
				viewSize = "0";
				_pView.setSizeValue("0");
				_pView.enableSizeValue(true);
			}
			_pData.setSizeValue(viewSize);
			_pData.setSizeUnit(viewUnit);
		}
	}

	private void checkIfDescriptionChanged() 
	{
		String viewDescription = _pView.getDescription();
		if (!viewDescription.equals(_pData.getDescription()))
		{
			_pData.setDescription(viewDescription);
		}
	}
}
